package com.springboot.hiresystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.hiresystem.domain.Companies;
import com.springboot.hiresystem.dto.req.CompaniesQueryRequestDTO;
import com.springboot.hiresystem.dto.req.CompaniesUpdateRequest;
import com.springboot.hiresystem.dto.resp.CompaniesResponseDTO;
import com.springboot.hiresystem.dto.resp.PageResponse;
import com.springboot.hiresystem.dto.resp.Result;
import com.springboot.hiresystem.service.CompaniesService;
import com.springboot.hiresystem.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/companies")
@Slf4j
public class CompaniesController {
    private CompaniesService companiesService;
    private UsersService usersService;

    public CompaniesController(CompaniesService companiesService, UsersService usersService) {
        this.companiesService = companiesService;
        this.usersService = usersService;
    }

// 带分页和条件查询
    @GetMapping("/list")
public Result<PageResponse<CompaniesResponseDTO>> list(CompaniesQueryRequestDTO companiesQueryRequestDTO) {
        System.out.println(
                "-------------------------------------123"
        );
    // 查询条件
    QueryWrapper<Companies> queryWrapper = new QueryWrapper<>();
    queryWrapper.like(companiesQueryRequestDTO.getCompanyName() != null, "username", companiesQueryRequestDTO.getCompanyName());

    // 分页信息
    Page<Companies> page = new Page<>(companiesQueryRequestDTO.getPage(), companiesQueryRequestDTO.getSize());
    // 分页查询
    Page<Companies> companiesPage = companiesService.page(page, queryWrapper);
    // 获取结果集
    List<Companies> companies = companiesPage.getRecords();
    log.info("查询结果:{}", companies);

    // 日志输出总记录数和总页数
    log.info("总记录数:{},总页数:{}", companiesPage.getTotal(), companiesPage.getPages());

    PageResponse<CompaniesResponseDTO> pageResponse = new PageResponse<>();
    // 转换成DTO
    List<CompaniesResponseDTO> companiesResponseDTOS = BeanUtil.copyToList(companies, CompaniesResponseDTO.class);
    pageResponse.setTotal(companiesPage.getTotal());
    pageResponse.setList(companiesResponseDTOS);
//    System.out.println("-----------------"+pageResponse.toString());

        // 返回结果
    return Result.success(pageResponse);
}

//     条件查询
    @PostMapping("/query")
    public Result<List<CompaniesResponseDTO>> query(@RequestBody CompaniesQueryRequestDTO companiesQueryRequestDTO) {
        QueryWrapper<Companies> queryWrapper = new QueryWrapper<>();

        if (companiesQueryRequestDTO.getCompanyName() != null) {
            queryWrapper.like("company_name", companiesQueryRequestDTO.getCompanyName());
        }
        List<Companies> companies = companiesService.list(queryWrapper);
        List<CompaniesResponseDTO> companiesResponseDTOS = BeanUtil.copyToList(companies, CompaniesResponseDTO.class); // 使用 BeanUtil.copyToList

        return Result.success(companiesResponseDTOS); // 自动设置 count
    }
    //更新
    @PostMapping("/update")
    public Result<String> update(@RequestBody CompaniesUpdateRequest companiesUpdateRequest) {
        Companies companies = BeanUtil.copyProperties(companiesUpdateRequest, Companies.class);
        companiesService.updateById(companies);
        return Result.success();
    }
    //添加
    @PostMapping("/add")
    public Result<String> add(@RequestBody Companies companies){
        System.out.println("--------------add"+companies.toString());
        //如果user表中未查找到userId对应的用户，则返回错误信息
//        System.out.println(usersService.getById(companies.getCreatedByUserId()).toString());
        if (usersService.getById(companies.getCreatedByUserId()) == null) {
            return Result.failure(400, "用户不存在");
        }
        companiesService.save(companies);
        return Result.success("添加成功");
    }
    //按照id删除
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody Companies companies) {
        companiesService.removeById(companies.getCompanyId());
        return Result.success("删除成功");
    }
    //按照id查询
    @PostMapping("/getByCompanyId")
    public Result<CompaniesResponseDTO> getById(@RequestBody Companies companies) {
        Companies company = companiesService.getById(companies.getCompanyId());
        CompaniesResponseDTO dto = BeanUtil.copyProperties(company, CompaniesResponseDTO.class);
        return Result.success(dto);
    }
}
